package com.ants.boot.test.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;


public class DyVideo2 {

    public static final String DEFAULT_URL = "D:\\data\\mp3\\";

    public static final String PRE_MP3 = ".mp3";



    public static void main(String[] args){
        String url = "https://www.9ku.com/playlist.php";
        String paramIds = "1386069%2C557590%2C662727%2C413197%2C586852%2C586851%2C515964%2C1002010%2C467647%2C586853%2C405805%2C464447%2C1010044%2C515965%2C462860%2C473019%2C364641%2C587604%2C411963%2C416478%2C409555%2C1014201%2C404298%2C1217852%2C586854%2C834969%2C829579%2C504911%2C1015487%2C411962%2C504089%2C369708%2C411964%2C586855%2C506078%2C587603%2C869265%2C1001502%2C1001503%2C863957%2C861950%2C669979%2C1014202%2C667784%2C1004128%2C873858%2C669977%2C587104%2C502606%2C1011762%2C1386179%2C587103%2C1387415%2C994471%2C1373387%2C866330%2C853636%2C1390855%2C1760776%2C1014795%2C1845263%2C872721%2C1537810%2C1362195%2C1811735%2C1638168%2C1611546%2C1673254%2C1371174%2C1381166%2C1396784%2C1403954%2C1547319%2C516167%2C1380936%2C1625416%2C1364552%2C1791827%2C1003612%2C1379677%2C1567843%2C1391972%2C1387886%2C1141617%2C1141619%2C1760371%2C1576823%2C1567608%2C1574009%2C1514622%2C1649536%2C1388928%2C1809031%2C1626508%2C1371023%2C1016721%2C1617562%2C1810331%2C1002654%2C1766052%2C1766054%2C1772455%2C1607592%2C1675432%2C467370%2C467371%2C1218992%2C878771%2C1675443%2C1570227%2C1594805%2C1578166%2C1012409%2C1095101%2C467648%2C1824192%2C1124041%2C1852363%2C1130444%2C1842902%2C1567958%2C1588440%2C1364961%2C1768676%2C1618151%2C1592554%2C1630698%2C1002476%2C1561587%2C1881340";
        getJkplay(url,paramIds);

    }

    /**
     * 九酷音乐
     * @param url
     * @param paramIds
     * @return
     */
    public static void getJkplay(String url,String paramIds){
        String params = "ids=%s";
        params = String.format(params, paramIds);
        String ret = PostUrl(url, params);
        JSONArray array = JSONArray.parseArray(ret);
        for (int i=0;i<array.size();i++){
            JSONObject obj = array.getJSONObject(i);
            String people = obj.getString("singer");
            String name = obj.getString("mname");
            String mp3Url = obj.getString("wma");
            System.out.println("演唱者是：" + people);
            System.out.println("音乐名称是：" + name);
            System.out.println("音乐地址是：" + mp3Url);
            downloadFile(mp3Url,new File(DEFAULT_URL + people + "--" + name + PRE_MP3));
        }
    }



    public static String PostUrl(String httpUrl, String param) {
        HttpURLConnection connection = null;
        InputStream is = null;
        OutputStream os = null;
        BufferedReader br = null;
        String result = null;
        try {
            URL url = new URL(httpUrl);
            // 通过远程url连接对象打开连接
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            // 默认值为：false，当向远程服务器传送数据/写数据时，需要设置为true
            connection.setDoOutput(true);
            // 默认值为：true，当前向远程服务读取数据时，设置为true，该参数可有可无
            connection.setDoInput(true);
            // 设置传入参数的格式:请求参数应该是 name1=value1&name2=value2 的形式。
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            // 通过连接对象获取一个输出流
            os = connection.getOutputStream();
            // 通过输出流对象将参数写出去/传输出去,它是通过字节数组写出的
            os.write(param.getBytes());
            // 通过连接对象获取一个输入流，向远程读取
            if (connection.getResponseCode() == 200) {

                is = connection.getInputStream();
                // 对输入流对象进行包装:charset根据工作项目组的要求来设置
                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));

                StringBuilder sbf = new StringBuilder();
                String temp = null;
                // 循环遍历一行一行读取数据
                while ((temp = br.readLine()) != null) {
                    sbf.append(temp);
                    sbf.append("\r\n");
                }
                result = sbf.toString();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != os) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            // 断开与远程地址url的连接

            assert connection != null;
            connection.disconnect();
        }
        return result;
    }


    /**
     * 下载远程文件至到本地
     *
     * @param remoteUrl
     * @throws IOException
     */
    public static void downloadFile(String remoteUrl ,File targetFile){
        try{
            // 构造URL
            URL url = new URL(remoteUrl);
            // 打开连接
            URLConnection con = url.openConnection();
            //添加这个东东 是为了防止抖音服务器拒绝报403
            con.addRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36");
            con.setConnectTimeout(50000);
            con.setReadTimeout(50000);
            // 输入流
            InputStream is = con.getInputStream();
            // 1K的数据缓冲
            byte[] bs = new byte[1024];
            // 读取到的数据长度
            int len;
            // 输出的文件流
            FileOutputStream os = new FileOutputStream(targetFile, true);
            while ((len = is.read(bs)) != -1) {
                os.write(bs, 0, len);
            }
            is.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }



}
